<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="./css/bootstrap-v4.6.0.css" />
  <link rel="stylesheet" href="./css/login.css" />
</head>

<body>
  <div class="login-box">
    <div class="form-group">
      <label for="username">Account</label>
      <!-- 账号 -->
      <input type="text" class="form-control" id="username" autocomplete="off" />
      <small id="emailHelp" class="form-text text-muted">The available account is <strong>admin</strong></small>
    </div>
    <div class="form-group">
      <!-- 密码 -->
      <label for="password">Password</label>
      <input type="password" class="form-control" id="password" />
      <small id="emailHelp" class="form-text text-muted">The available password is <strong>123456</strong></small>
    </div>
    <button type="submit" class="btn btn-primary" id="btnLogin">
      Submit
    </button>
  </div>

  <script src="./lib/axios.js"></script>

  <script>
    const username1 = document.querySelector('#username')
    const password1 = document.querySelector('#password')
    const btnLogin = document.querySelector('#btnLogin')

    btnLogin.addEventListener('click',function(){
      let username = username1.value
      let password = password1.value

      axios({
        url:'http://ajax-api.itheima.net/api/login',
        method:'POST',
        data:{
          username,
          password,
        }
      }).then(({data:res})=>{
        let data = res
        if(data.message !== '登录成功'){
          return alert('登入失败，请检查您的账号密码')
        } 
      })
    })



/*     // 请求方式 POST
    // 地址 http://ajax-api.itheima.net/api/login
    // 参数:  username 用户名     password  密码  ==> 请求体参数，写在data中

    // 步骤
    //  1. 给按钮注册click
    //  2. 获取输入框的值 ==> value属性
    //  3. 使用axios发送请求，之后根据响应结果，做提示

    //1.绑定点击事件
    const submitBtn = document.querySelector("#btnLogin");

    submitBtn.addEventListener("click", function () {
      //获取输入框的值
      const username = document.querySelector("#username").value;
      const password = document.querySelector("#password").value;

      //发起请求
      axios({
        method: "POST",
        url: "http://ajax-api.itheima.net/api/login",
        data: {
          username,
          password,
        },
      })
        .then((res) => {
          const { data } = res;
          console.log(data);
          //登录失败
          if (data.message !== "登录成功")
            return alert("登录失败，请检查您的账号密码");
          //登录成功
          alert("登录成功！欢迎您" + data.data.account);
        })
        .catch((err) => {
          //如果不需要在控制台上显示报错可以使用console.clear清除报错
          console.clear();
          //通过catch方法捕获错误信息
          alert("登录失败，请检查您的账号密码");
        });
    }); */
  </script>
</body>

</html>